package com.ruoyi.system.outto.game.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.model.domain.AccountInfo;
import com.ruoyi.system.model.domain.InfoImg;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.model.mapper.InfoImgMapper;
import com.ruoyi.system.model.mapper.SuprAuthMapper;
import com.ruoyi.system.outto.core.LCConstant;
import com.ruoyi.system.outto.core.MapUntils;
import com.ruoyi.system.outto.core.OutUrlConstant;
import com.ruoyi.system.outto.core.RegionalCovert;
import com.ruoyi.system.outto.game.CommonApi;
import com.ruoyi.system.outto.game.LCAPI;
import com.ruoyi.system.outto.game.QSHANAPI;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lz
 * @version 1.0
 * @date 2022/6/5 16:19
 */
@Slf4j
@Service
public class QSHANAPIImpl implements QSHANAPI {

    @Resource
    private InfoImgMapper infoImgMapper;
    @Value("${desc.title}")
    private String descTitle;
    private SalePartner salePartner;
    @Resource
    private CommonApi commonApi;

    @Override
    public AjaxResult addGame(AccountInfo accountInfo, Long userId,SalePartner salePartner) {
        this.salePartner = salePartner;
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> params = params(accountInfo, salePartner);
        log.info("青山添加接口请求参数={}", JSONObject.toJSONString(params));
        String response = HttpUtils.doPostForm(salePartner.getTwoOther() + "/index/index/thirdGoodsInsert", params, null);
        log.info("青山添加接口返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);
        if (!"1".equals(resultJson.getString("code"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
        }

        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", resultJson.getString("data"));
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult editGame(AccountInfo accountInfo, Long userId,SalePartner salePartner) {
        this.salePartner = salePartner;
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> params = params(accountInfo, salePartner);
        log.info("青山编辑接口请求参数={}", JSONObject.toJSONString(params));
        String response = HttpUtils.sendPost(salePartner.getTwoOther() + "/index/index/thirdGoodsEditPrice", JSONObject.toJSONString(params), "application/json");
        log.info("青山编辑接口返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);

        if (!"1".equals(resultJson.getString("code"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
        }


        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", resultJson.getString("msg"));
        return AjaxResult.success(result);
    }


    private Map<String, Object> params(AccountInfo accountInfo, SalePartner sa) {

        String des = accountInfo.getDes();
        des = "xyr|" + accountInfo.getId() + " " + accountInfo.getLevel() + " " + accountInfo.getRegional() + des;
        accountInfo.setDes(des);

        Map<String,Object> params = new HashMap<>();
        params.put("user_id",this.salePartner.getSaleKey());
        if (Objects.equals("王者荣耀",accountInfo.getType())){
            params.put("game_id","13");
            if (Objects.equals(accountInfo.getRegional(),"安卓WX")){
                params.put("area_id","62");
            }else if (Objects.equals(accountInfo.getRegional(),"安卓Q")){
                params.put("area_id","61");
            }else if (Objects.equals(accountInfo.getRegional(),"苹果Q")){
                params.put("area_id","63");
            }else if (Objects.equals(accountInfo.getRegional(),"苹果WX")){
                params.put("area_id","64");
            }

        }else if (Objects.equals("和平精英",accountInfo.getType())){
            params.put("game_id","15");
            if (Objects.equals(accountInfo.getRegional(),"安卓WX")){
                params.put("area_id","73");
            }else if (Objects.equals(accountInfo.getRegional(),"安卓Q")){
                params.put("area_id","74");
            }else if (Objects.equals(accountInfo.getRegional(),"苹果Q")){
                params.put("area_id","76");
            }else if (Objects.equals(accountInfo.getRegional(),"苹果WX")){
                params.put("area_id","77");
            }
        }else if (Objects.equals("英雄联盟",accountInfo.getType())){
            if (Arrays.asList(RegionalCovert.dianxin).contains(accountInfo.getRegional())){
                params.put("area_id","84");
            }else if (Arrays.asList(RegionalCovert.wangtong).contains(accountInfo.getRegional())){
                params.put("area_id","85");
            }else if (Objects.equals("男爵领域",accountInfo.getRegional())){
                params.put("area_id","82");
            }else if (Objects.equals("峡谷之巅",accountInfo.getRegional())){
                params.put("area_id","83");
            }
            params.put("game_id","16");
        }else if (Objects.equals("CF端游",accountInfo.getType())){
            if (Objects.equals("CFHD大区",accountInfo.getRegional())){
                params.put("area_id","130");
            }
            if (Objects.equals("西部大区",accountInfo.getRegional())){
                params.put("area_id","129");
            }
            if (Objects.equals("北部大区",accountInfo.getRegional())){
                params.put("area_id","128");
            }
            if (Objects.equals("南部大区",accountInfo.getRegional())){
                params.put("area_id","127");
            }
            if (Objects.equals("东部大区",accountInfo.getRegional())){
                params.put("area_id","126");
            }
            params.put("game_id","21");
        }else if (Objects.equals("CF手游",accountInfo.getType())){
            if (Objects.equals("安卓Q",accountInfo.getRegional())){
                params.put("area_id","121");
            }
            if (Objects.equals("苹果Q",accountInfo.getRegional())){
                params.put("area_id","120");
            }
            if (Objects.equals("苹果WX",accountInfo.getRegional())){
                params.put("area_id","122");
            }
            if (Objects.equals("安卓WX",accountInfo.getRegional())){
                params.put("area_id","124");
            }
            params.put("game_id","20");
        }else if (Objects.equals("火影忍者",accountInfo.getType())){
            if (Objects.equals("安卓Q",accountInfo.getRegional())){
                params.put("area_id","137");
            }
            if (Objects.equals("苹果Q",accountInfo.getRegional())){
                params.put("area_id","136");
            }
            if (Objects.equals("苹果WX",accountInfo.getRegional())){
                params.put("area_id","134");
            }
            if (Objects.equals("安卓WX",accountInfo.getRegional())){
                params.put("area_id","135");
            }
            params.put("game_id","22");
        }else if (Objects.equals("原神",accountInfo.getType())){
            if (Objects.equals("官服",accountInfo.getRegional())){
                params.put("area_id","131");
            }
            if (Objects.equals("B服",accountInfo.getRegional())){
                params.put("area_id","132");
            }
            params.put("game_id","19");
        }else {
            throw new GlobalException("此平台暂未开通此游戏:" + accountInfo.getType());
        }


        params.put("goods_no",descTitle + "|" + accountInfo.getId());

        params.put("game_accout","2183411111");
        params.put("game_password","2183422222");
        if (des.length() > 599){
            des = des.substring(0,99);
            params.put("goods_name",des);
        }else {
            params.put("goods_name",des);
        }

        InfoImg infoImg = new InfoImg();
        infoImg.setInfoId(accountInfo.getId());
        infoImg.setType(Constants.STR_1);
        List<InfoImg> infoImgs = infoImgMapper.selectInfoImgList(infoImg);

        String imgs = "";
        if (Objects.nonNull(accountInfo.getTitleImg())){
            imgs = accountInfo.getTitleImg() + ",";
        }
        if (!CollectionUtils.isEmpty(infoImgs)) {
            imgs = imgs + infoImgs.stream().map(i -> i.getImgUrl()).collect(Collectors.joining(","));
        } else {
            imgs =  imgs + LCConstant.defaultImg;
        }

        if (StringUtils.isNotEmpty(accountInfo.getTitleImg())){
            params.put("cover_images",accountInfo.getTitleImg());
        }else {
            if (CollectionUtils.isNotEmpty(infoImgs)){
                params.put("cover_images",infoImgs.get(0));
            }else {
                params.put("cover_images",LCConstant.defaultImg);
            }
        }

        params.put("role_id","");
        params.put("is_bao","1");
        params.put("is_sign_contract","1");
        params.put("detail","1");
        params.put("detail_images",imgs);
        params.put("is_bargain","1");
        BigDecimal bigDecimal = commonApi.addPrice(accountInfo.getPrice(),  sa.getSeleep());
        commonApi.setPrice(bigDecimal,accountInfo.getId(), sa);
        params.put("price",bigDecimal);
        params.put("qq","1111111");
        params.put("wechat","1111111");
        params.put("mobile","1111111");
        params.put("remark","");
        return params;
    }


    @Override
    public AjaxResult delGame(Long id, Long userId,SalePartner sa) {
        Map<String,Object> params = new HashMap<>();
        params.put("goods_no","xyr|" + id);
        log.info("青山删除接口请求参数={}", JSONObject.toJSONString(params));
        String response = HttpUtils.doPostForm(sa.getTwoOther() + "/index/index/thirdGoodsDown", params, null);
        if (StringUtils.isEmpty(response)) {
            throw new GlobalException("请求接口失败");
        }
        log.info("青山删除接口返回参数={}", JSONObject.toJSONString(response));
        JSONObject resultJson = JSONObject.parseObject(response);
        Map<String, Object> result = new HashMap<>();

        if (!"1".equals(resultJson.getString("code"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
        }

        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", resultJson.getString("msg"));
        return AjaxResult.success(result);
    }



    private String getRea(){
        Map<String, Object> params = new TreeMap<>();
        params.put("platform","xtdanbao");

        String cover = MapUntils.getCover(params);
        cover = cover + "key=wvvbt1rjwvms7o7e319jm7p28jhbqdc3";
        String sign = DigestUtils.md5Hex(cover).toUpperCase();
        params.put("sign", sign);
//        String response = HttpUtils.sendPost(OutUrlConstant.LCADD, JSONObject.toJSONString(params),"application/json");
        String response = HttpUtils.sendPost("https://www.51youxihao.com/index.php?g=api&m=product&a=list_games", JSONObject.toJSONString(params), "application/json");
        System.out.println(response);
        return response;
    }

    public static void main(String[] args) {

        String url = "https://zhiyuanhuyu.niurenjianzhan.com";

//        Map<String,Object> params = new HashMap<>();
//        params.put("user_id","21834");
//        params.put("game_id","13");
//        params.put("goods_no","xyr|21834");
//        params.put("area_id","64");
//        params.put("game_accout","2183411111");
//        params.put("game_password","2183422222");
//        params.put("goods_name","xyr21834hahahahahh");
//        params.put("cover_images",LCConstant.defaultImg);
//        params.put("role_id","21834");
//        params.put("is_bao","1");
//        params.put("is_sign_contract","1");
//        params.put("detail","1");
//        params.put("detail_images",LCConstant.defaultImg);
//        params.put("is_bargain","1");
//        params.put("price","99");
//        params.put("qq","1111111");
//        params.put("wechat","1111111");
//        params.put("mobile","1111111");
//        params.put("remark","");
//        System.out.println(JSON.toJSONString(params));
//        String postForm = HttpUtils.doPostForm(url + "/index/index/thirdGoodsInsert", params, null);
//        System.out.println(postForm);

        Map<String,Object> params = new HashMap<>();
        params.put("goods_no","xyr|21834");
        System.out.println(JSON.toJSONString(params));
        String postForm = HttpUtils.doPostForm(url + "/index/index/thirdGoodsDown", params, null);
        System.out.println(postForm);


    }

}
